<?php

use App\Models\AssetStockChangeHistoryAction;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAssetStockChangeHistoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('asset_stock_change_history_actions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string("name");
            $table->timestamps();
        });
        Schema::create('asset_stock_change_histories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger("action_id");
            $table->unsignedInteger('asset_stock_id');
            $table->string("comment")->nullable();
            $table->integer('quantity');
            $table->unsignedInteger("user_id");
            $table->timestamps();
            $table->softDeletes();
            $table->foreign("action_id")
                ->references("id")
                ->on("asset_stock_change_history_actions");
            $table->foreign('asset_stock_id')
                ->references('id')
                ->on('asset_stocks');
            $table->foreign('user_id')
                ->references('id')
                ->on('users');
        });
        // init default change action
        $seeds = ["添加", "报废", "维修"];
        foreach ($seeds as $seed) {
            AssetStockChangeHistoryAction::create([
                "name" => $seed
            ]);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('asset_stock_change_histories');
        Schema::dropIfExists('asset_stock_change_history_actions');

    }
}
